package com.ihemou.system.controller;

import com.ihemou.common.core.domain.R;
import com.ihemou.common.core.utils.poi.ExcelUtil;
import com.ihemou.common.core.web.controller.BaseController;
import com.ihemou.common.core.web.domain.AjaxResult;
import com.ihemou.common.core.web.page.TableDataInfo;
import com.ihemou.common.log.annotation.Log;
import com.ihemou.common.log.enums.BusinessType;
import com.ihemou.common.security.annotation.PreAuthorize;
import com.ihemou.common.security.annotation.ServiceRequest;
import com.ihemou.system.domain.SysSocialUser;
import com.ihemou.system.domain.SysUser;
import com.ihemou.system.dto.SysUserDTO;
import com.ihemou.system.dto.UserInfo;
import com.ihemou.system.dto.WxUserInfoDTO;
import com.ihemou.system.service.ISysPermissionService;
import com.ihemou.system.service.ISysSocialUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Set;


/**
 * All rights Reserved, Designed By saperliu
 * Copyright:    Copyright(C) 2021-2021
 *
 * @author saperliu
 * @version 1.0.0
 * @date 2021年09月02日
 * @Description 社交账号的接口
 */
@Slf4j
@RestController
public class SysSocialUserController extends BaseController {

    @Autowired
    private ISysSocialUserService sysSocialUserService;

    @Autowired
    private ISysPermissionService permissionService;

    /**
     * 新增社交账号
     */
    @ServiceRequest
    @Log(title = "社交账号", businessType = BusinessType.INSERT)
    @PostMapping("/inner/social/sysSocialUser/save")
    public R<UserInfo> addSocialUser(@RequestBody WxUserInfoDTO wxUserInfoDTO) {

        SysUser sysUser = sysSocialUserService.saveWxSocialUser(wxUserInfoDTO);
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(sysUser.getId());
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(sysUser.getId());
        UserInfo sysUserVo = new UserInfo();
        SysUserDTO userDTO = new SysUserDTO();
        BeanUtils.copyProperties(sysUser, userDTO);
        sysUserVo.setSysUser(userDTO);
        sysUserVo.setRoles(roles);
        sysUserVo.setPermissions(permissions);
        sysUserVo.setUserId(sysUser.getId());
        sysUserVo.setOpenId(wxUserInfoDTO.getOpenId());
        return R.ok(sysUserVo);
    }

    /**
     * 新增社交账号
     */
    @ServiceRequest
    @Log(title = "社交账号自动登录", businessType = BusinessType.UPDATE)
    @PostMapping("/inner/social/sysSocialUser/find")
    public R<UserInfo> findSocialUser(@RequestBody WxUserInfoDTO wxUserInfoDTO) {

        SysUser sysUser = sysSocialUserService.findWxSocialUser(wxUserInfoDTO);
        if(sysUser==null){
            return R.fail("未找到此用户");
        }else{
            // 角色集合
            Set<String> roles = permissionService.getRolePermission(sysUser.getId());
            // 权限集合
            Set<String> permissions = permissionService.getMenuPermission(sysUser.getId());
            UserInfo sysUserVo = new UserInfo();
            SysUserDTO userDTO = new SysUserDTO();
            BeanUtils.copyProperties(sysUser, userDTO);
            sysUserVo.setSysUser(userDTO);
            sysUserVo.setRoles(roles);
            sysUserVo.setPermissions(permissions);
            sysUserVo.setUserId(sysUser.getId());
            sysUserVo.setOpenId(wxUserInfoDTO.getOpenId());
            return R.ok(sysUserVo);
        }
    }

    /**
     * 查询社交账号列表
     */
    @PreAuthorize(hasPermi = "social:sysSocialUser:list")
    @GetMapping("/back/social/sysSocialUser/list")
    public TableDataInfo list(SysSocialUser sysSocialUser) {
        startPage();
        List<SysSocialUser> list = sysSocialUserService.findSysSocialUserByCondition(sysSocialUser);
        return getDataTable(list);
    }

    /**
     * 导出社交账号列表
     */
    @PreAuthorize(hasPermi = "social:sysSocialUser:export")
    @Log(title = "社交账号", businessType = BusinessType.EXPORT)
    @PostMapping("/back/social/sysSocialUser/export")
    public void export(HttpServletResponse response, SysSocialUser sysSocialUser) throws IOException {
        List<SysSocialUser> list = sysSocialUserService.findSysSocialUserByCondition(sysSocialUser);
        ExcelUtil<SysSocialUser> util = new ExcelUtil<SysSocialUser>(SysSocialUser.class);
        util.exportExcel(response, list, "社交账号数据");
    }

    /**
     * 获取社交账号详细信息
     */
    @PreAuthorize(hasPermi = "social:sysSocialUser:query")
    @GetMapping(value = "/back/social/sysSocialUser/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(sysSocialUserService.selectSysSocialUserById(id));
    }

    /**
     * 新增社交账号
     */
    @PreAuthorize(hasPermi = "social:sysSocialUser:add")
    @Log(title = "社交账号", businessType = BusinessType.INSERT)
    @PostMapping("/back/social/sysSocialUser/save")
    public AjaxResult add(@RequestBody SysSocialUser sysSocialUser) {
        return toAjax(sysSocialUserService.insertSysSocialUser(sysSocialUser));
    }

    /**
     * 修改社交账号
     */
    @PreAuthorize(hasPermi = "social:sysSocialUser:edit")
    @Log(title = "社交账号", businessType = BusinessType.UPDATE)
    @PutMapping("/back/social/sysSocialUser/update")
    public AjaxResult edit(@RequestBody SysSocialUser sysSocialUser) {
        return toAjax(sysSocialUserService.updateSysSocialUser(sysSocialUser));
    }

    /**
     * 删除社交账号
     */
    @PreAuthorize(hasPermi = "social:sysSocialUser:remove")
    @Log(title = "社交账号", businessType = BusinessType.DELETE)
    @DeleteMapping("/back/social/sysSocialUser/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sysSocialUserService.deleteSysSocialUserByIds(ids));
    }
}
